package com.hl.card.common.constant.factory;

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.constant.state.Order;
import com.hl.card.core.support.HttpKit;
import com.hl.card.core.util.ToolUtil;

/**
 * BootStrap Table默认的分页参数创建
 *
 * @author fengshuonan
 * @date 2017-04-05 22:25
 */
public class PageFactory<T> {

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpKit.getRequest();
        if (request.getParameter("current")!=null && request.getParameter("size")!=null) {
        	return Page();
		}else {
			int limit = Integer.valueOf(request.getParameter("limit"));
			int offset = Integer.valueOf(request.getParameter("offset"));
			String sort = request.getParameter("sort");
			String order = request.getParameter("order");
			if(ToolUtil.isEmpty(sort)){
				Page<T> page = new Page<>((offset / limit + 1), limit);
				page.setOpenSort(false);
				return page;
			}else{
				Page<T> page = new Page<>((offset / limit + 1), limit, sort);
				if(Order.ASC.getDes().equals(order)){
					page.setAsc(true);
				}else{
					page.setAsc(false);
				}
				return page;
		}
        }
    }
    public Page<T> Page() {
    	HttpServletRequest request = HttpKit.getRequest();
    	int current = Integer.valueOf(request.getParameter("current"));
    	int size = Integer.valueOf(request.getParameter("size"));
    	String sort = request.getParameter("sort");
    	String order = request.getParameter("order");
    	if(ToolUtil.isEmpty(sort)){
    		Page<T> page = new Page<>(current, size);
    		page.setOpenSort(false);
    		return page;
    	}else{
    		Page<T> page = new Page<>(current, size, sort);
    		if(Order.ASC.getDes().equals(order)){
    			page.setAsc(true);
    		}else{
    			page.setAsc(false);
    		}
    		return page;
    	}
    }
}
